<?php

/**
 * 微信投票权手 Controller
 * 
 * @path Apps/Mp/Controller/WxEventVotePlayerController.class.php
 * @author Zix <zix2002@gmail.com> 
 * @version v1.0 , 2015-09-11
 */

namespace Mp\Controller;

use Common\Service\WxEventVotePlayerService;

class WxEventVotePlayerController extends MpController {

  public function index( $event_id ) {
    $action = I('get.action', 'read');

    $data = $this->_init_data();
    $data['module_name'] = '投票参数权手';
    $data['module_url'] = U( $this->base_path . 'index');

    $WxEventVotePlayer = WxEventVotePlayerService::instance();
    $data['config'] = array(
      'action' => $action,
      'action_label' => $this->action_label[$action],
      'menu_id'    => 'menu_Mp_MerWxSetting_index' ,
      'event_id'   => $event_id ,
      'module_url' => U($this->base_path . 'index'),
      'read_url'   => U($this->base_path . 'read' ) . "/$event_id",
      'create_url' => U($this->base_path . 'create'),
      'update_url' => U($this->base_path . 'update'),
      'delete_url' => U($this->base_path . 'delete'),
      'upload_icon_url' => U($this->base_path . 'upload') .'/icon',
      'del_icon_url' => U($this->base_path . 'delete_icon') ,
      'attach_url' => U('Mp/Attachments/index') . "/menu_Mp_MerWxSetting_index/101/",
      'search_user_url' => U($this->base_path . 'search_user') ,
      'page' => 1,
      'page_size' => C('PAGE_SIZE'),
      'row' => $WxEventVotePlayer->get_default_row() 
    );

    $data['param'] = array(
      'status' => $WxEventVotePlayer->status ,
      'sex'    => $WxEventVotePlayer->sex ,
    );

    $this->display_with_layout($data, $this->controller .':index');
  }

  
  function read( $event_id ) {
    $post = I('post.');

    $config = array(
      'event_id' => $event_id ,
      'page' => $post['page'],
      'page_size' => $post['page_size'],
      'sort' => empty($post['sort']) ? 'id' : $post['sort'],
      'order' => empty($post['order']) ? 'DESC' : $post['order'],
    );

    $WxEventVotePlayer = WxEventVotePlayerService::instance();
    $data['rows'] = $WxEventVotePlayer->get_by_cond($config);

    $config['count'] = TRUE;
    $data['total'] = $WxEventVotePlayer->get_by_cond($config);

    $this->ajaxReturn($data);
  }
  
  function search_user() {
    $MerUser = \Common\Service\MerUserService::instance();
    
    $data = $MerUser->get_by_cond(array(
      'keyword' => I('get.keyword'),
      'page' => I('get.page' , 1 ) ,
      'page_size' => I('get.page_size'),
    ));
    
    $this->ajaxReturn($data);
  }
  
  function create() {
    $post = I('post.');
    if (empty($post)) {
      $this->ajaxReturn(ajax_arr('数据错误,请稍后再试'));
    }
    
    $WxEventVotePlayer = WxEventVotePlayerService::instance();
    $ret = $WxEventVotePlayer->create($post);
    $this->ajaxReturn($ret);
  }

  function update($id = 0) {
    $post = I('post.');
    if (empty($post)) {
      $this->ajaxReturn(ajax_arr('数据错误,请稍后再试'));
    }

    $WxEventVotePlayer = WxEventVotePlayerService::instance();
    $ret = $WxEventVotePlayer->update($id, $post);
    $this->ajaxReturn($ret);
  }

  function delete() {
    $ids = I('post.ids');
    $WxEventVotePlayer = WxEventVotePlayerService::instance();
    $ret = $WxEventVotePlayer->delete($ids); 
    $this->ajaxReturn($ret);
  }
}
